package com.sean.item;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class LoadingLayout extends RelativeLayout {

	private boolean mIsLoading = true;
	private ProgressBar mProgressBar;
	
	public LoadingLayout(Context context) {
		super(context);
	}

	public LoadingLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		
	}
	
	public void loading() {
		if(mProgressBar == null) {
			mProgressBar = new ProgressBar(this.getContext());
			LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			lp.addRule(RelativeLayout.CENTER_IN_PARENT);
			this.addView(mProgressBar, lp);
		}
		
		mIsLoading = true;
		mProgressBar.setVisibility(View.VISIBLE);
	}
	
	public void endLoading() {
		mIsLoading = false;
		mProgressBar.setVisibility(View.GONE);
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		if(mIsLoading) {
			canvas.drawColor(Color.parseColor("#55000000"));
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if(mIsLoading)
			return true;
		return super.dispatchTouchEvent(ev);
	}
	
}
